package com.attilax.secury;
import net.sf.json.JSONObject;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

















import com.attilax.Closure;
import com.attilax.io.filex;
import com.attilax.util.dirx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * 计算大文件MD5 
 *  David  2012-10-12
 */
public class MD5_BigFile {
   
    static MessageDigest MD5 = null;


    static {
        try {
        MD5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException ne) {
        ne.printStackTrace();
        }
    }


    /**
     * 对一个文件获取md5值
     * @return md5串
     * @throws geneEX 
     */
    public static String getMD5(File file) throws geneEX {
        FileInputStream fileInputStream = null;
        try {
        fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int length;
            while ((length = fileInputStream.read(buffer)) != -1) {
            MD5.update(buffer, 0, length);
            }


            return new String(Hex.encodeHex(MD5.digest()));
        } catch (FileNotFoundException e) {
        e.printStackTrace();
           throw new geneEX("FileNotFoundException");
        } catch (IOException e) {
        e.printStackTrace();
        throw new geneEX("IOException");
        } finally {
            try {
                if (fileInputStream != null)
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 求一个字符串的md5值
     * @param target 字符串
     * @return md5 value
     */
    public static String MD5(String target) {
        return DigestUtils.md5Hex(target);
    }

  static  int i=0;
	
    /**
     * -output_file:"c:/p88_md5.txt -dir:"E:\ati\garbalFileMd5"
     * E:\ati\0delM5
     * @param args
     * @throws geneEX
     * @throws IOException
     * @throws ParseException 
     */
    public static void main(String[] args) throws geneEX, IOException, ParseException{
    	
    	// t(args);
    String s=	MD5_BigFile.getMD5(new File("c:\\aa.txt"));
    System.out.println("ok:"+s);
	
	
    }


	private static void t(String[] args) throws ParseException, IOException {
		Options opts = new Options(); 
         opts.addOption("output_file", true, "help:pls input output ");
		 opts.addOption("dir", true,"help:pls input dir  "); 
	 
		 CommandLineParser parser = new PosixParser(); 
		 CommandLine cmd = parser.parse(opts, args); 

//		 if(cmd.hasOption("h")) { 
//		    // 这里显示简短的帮助信息
//		 }
   
    long beginTime =System.currentTimeMillis();
    //  String json = args[0];
   //   JSONObject jo=JSONObject.fromObject(json);      
	String dir = cmd.getOptionValue("dir");
			//jo.getString("dir");
	String output =cmd.getOptionValue("output_file");
	final filex fx=new filex(output,"gbk");
	
	dirx.trave(dir, new Closure () {

		@Override
		public Object execute(Object arg0) throws Exception {
			if(i==0)
			 fx.appendLine_flush_safe("\r\n"); 
			i++;
			System.out.println(" now process ing:"+String.valueOf(i));
			String f=(String) arg0;
			File fileZIP = new File(f);
		      String md5=getMD5(fileZIP);
		      System.out.println(md5);
		      long endTime =System.currentTimeMillis();
		//     System.out.println("MD5:"+md5+"\n time:"+((endTime-beginTime)/1000)+"s");
		      fx.appendLine_flush_safe(md5+","+fileZIP.length()); 
			return null;
		}
	});
	fx.close();
	System.out.println("--f");
	}
}